<template>
  <div class="createPost-container">
    <el-form v-if="!infoLoading" ref="postForm" :model="postForm" :rules="rules" class="form-container">
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="24">
            <el-form-item style="margin-bottom: 40px;" prop="roleName">
              <MDinput v-model="postForm.roleName" :maxlength="100" name="name" required>
                角色名称
              </MDinput>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item prop="type" style="margin-bottom: 30px;">
          <el-switch
            v-model="postForm.type"
            style="display: block"
            active-color="#13ce66"
            inactive-color="#409eff"
            active-text="后台角色"
            inactive-text="前台角色"
            @change="getMenuList()"
          />
        </el-form-item>
        <el-form-item prop="remark" style="margin-bottom: 30px;" label="权限">
          <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">

            <el-table-column min-width="300px" prop="dept_name" label="菜单名称" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.menuCode>100&&postForm.type?'——':'' }}{{ scope.row.menuName }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="400">
              <template slot-scope="scope">
                <el-checkbox-group v-if="scope.row.isLeaf" v-model="scope.row.actionTypes">
                  <el-checkbox v-for="(item,index) in actionTypes" :key="scope.row.id*100+index" :label="item.value" @change="changeMyList(scope.$index)">{{ item.label }}</el-checkbox>
                </el-checkbox-group>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item style="margin-bottom: 30px;">
          <el-button type="primary" :loading="loading" @click="submitForm">提交</el-button>
        </el-form-item>

      </div>
    </el-form>
  </div>
</template>

<script>
import MDinput from '@/components/MDinput'

const defaultForm = {
  id: null,
  roleName: '',
  type: true
}

export default {
  name: 'SysDeptDetail',
  components: { MDinput },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    infoid: {
      type: Number,
      default: 0
    },
    timestamp: {
      type: Number,
      default: 0
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: '请将表单填写完整',
          type: 'error'
        })
        callback(new Error('必填项'))
      } else {
        callback()
      }
    }
    return {
      testactionTypes: [],
      actionTypes: [],
      list: [],
      listLoading: false,
      infoLoading: false,
      postForm: Object.assign({}, defaultForm),
      loading: false,
      rules: {
        roleName: [{ validator: validateRequire }]
      },
      tempRoute: {}
    }
  },
  watch: {
    timestamp: {
      handler(val, oldVal) {
        console.log('val:' + val)
        this.getMenuList()
        this.getData(this.infoid)
      },
      immediate: true
    }
  },
  mounted() {
  },
  created() {
    // Why need to make a copy of this.$route here?
    // Because if you enter this page and quickly switch tag, may be in the execution of the setTagsViewTitle function, this.$route is no longer pointing to the current page
    // https://github.com/PanJiaChen/vue-element-admin/issues/1221
    this.tempRoute = Object.assign({}, this.$route)
  },
  methods: {
    changeMyList(index) { // 解决点击响应不同步的问题
      const tmpObj = this.list[index]
      this.$set(this.list, index, tmpObj)
    },
    getMenuList() {
      this.listLoading = true
      const type = this.postForm.type ? 1 : 2
      this.$http
        .get('/api/sysMenu/getMenuList?type=' + type)
        .then(response => {
          this.listLoading = false
          if (response.data.success === true) {
            this.list = response.data.result
            this.list.forEach(item => {
              item.actionTypes = []
            })
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.listLoading = false
        })

      this.$http
        .get('/api/sysDict/getByCode?code=03')
        .then(response => {
          if (response.data.success === true) {
            this.actionTypes = response.data.result
          // this.total = response.data.result.total
          // if (response.data.result.current > response.data.result.pages && response.data.result.pages !== 0) {
          //   this.listQuery.page = response.data.result.pages
          //   this.getList()
          // }
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
        })

      // fetchList(this.listQuery).then(response => {
      //   this.list = response.data.items
      //   this.total = response.data.total
      //   this.listLoading = false
      // })
    },
    getData(id) {
      if (id) {
        this.infoLoading = true
        this.$http
          .get('/api/sysDept/getById?&id=' + id)
          .then(response => {
            this.infoLoading = false
            if (response.data.success === true) {
              this.postForm = response.data.result
            } else {
              console.log('获取失败:' + response.data.message)
              this.$message({
                message: '获取失败:' + response.data.message,
                type: 'error'
              })
            }
          })
          .catch(error => {
            console.log(error)
            this.infoLoading = false
          })
      } else {
        this.postForm = Object.assign({}, defaultForm)
      }
    },
    submitForm() {
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          const para = {
            roleName: this.postForm.roleName,
            type: this.postForm.type,
            id: this.postForm.id ? this.postForm.id : null
          }
          this.loading = true
          this.$http
            .post('/api/sysDept/save', para)
            .then(response => {
              this.loading = false
              if (response.data.success === true) {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.$emit('dialogHide')
              } else {
                console.log('提交失败:' + response.data.message)
                this.$message({
                  message: '提交失败:' + response.data.message,
                  type: 'error'
                })
              }
            })
            .catch(function(error) {
              console.log(error)
              this.loading = false
            })
          this.postForm.status = 'published'
          this.postForm = Object.assign({}, defaultForm)
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    // padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
